package cn.itcast.service;

import cn.itcast.constant.MessageConstant;
import cn.itcast.interfaces.OrderService;
import cn.itcast.interfaces.OrderSettingService;
import cn.itcast.mapper.MemberMapper;
import cn.itcast.mapper.OrderMapper;
import cn.itcast.mapper.OrderSettingMapper;
import cn.itcast.pojo.Member;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.pojo.Result;
import cn.itcast.utils.DateUtils;
import cn.itcast.utils.SMSUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: peterChen
 * @date: 2019/8/14 18:52
 * @description: TODO
 * @version: 1.0
 */

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    /**
     * 微信预约
     * @param map
     * @return
     */
    @Override
    public Result order(Map map) throws Exception {
        //根据日期确认是否设置可预约人数
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting  = orderSettingMapper.findOrderSettingByOrderDate(date);
        if(ObjectUtils.isEmpty(orderSetting)){
            //未设置可预约人数
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //判断已预约人数是否已满
        if(orderSetting.getReservations()>=orderSetting.getNumber()){
            //预约已满
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //判断是否是会员-根据手机号判断
        String telephone = (String) map.get("telephone");
        Member member = memberMapper.findMemberByTelePhone(telephone);

        //防止重复预约
        if(!ObjectUtils.isEmpty(member)){
            //是会员，确定预约日期是否有预约，且预约了统一套餐
            Integer setmealId = Integer.parseInt((String) map.get("id"));   //预约套餐id
            Integer memberId = member.getId();  //会员已预约的套餐id
            //调用dao层，确认是否已预约
            Order order = new Order(memberId, date, null, null, setmealId);
            List<Order> list = orderMapper.findByCondition(order);
            if(!CollectionUtils.isEmpty(list) && list.size() > 0){
                //已预约,不能重复预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }

        //可以预约，设置已预约次数 + 1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingMapper.editReservationsByDate(orderSetting);

        if(ObjectUtils.isEmpty(member)){
            //不是会员，自动完成注册会员
            member = new Member();
            String idCard = (String) map.get("idCard");
            String name = (String) map.get("name");
            String sex = (String) map.get("sex");
            member.setPhoneNumber(telephone);
            member.setIdCard(idCard);
            member.setName(name);
            member.setSex(sex);
            member.setRegTime(new Date());
            memberMapper.add(member);
        }

        //将预约信息添加进order预约信息表中
        Order order = new Order(member.getId(),date,(String)map.get("orderType"),Order.ORDERSTATUS_NO,Integer.parseInt((String) map.get("id")));
        orderMapper.add(order);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order);
    }

    /**
     * 根据id查询预约信息（会员名、套餐名、体检类型、预约日期）
     * @param id
     * @return
     */
    @Override
    public Map findById(Integer id) {
       return orderMapper.findById(id);

    }
}
